﻿using System;
using System.Windows;
using System.Windows.Controls;
using GpgApi;

namespace WgpgGUI
{
    public partial class DialogGenerateKey : Window
    {
        private DateTime m_mindate;

        public DialogGenerateKey(Window owner)
        {
            Owner = owner;

            InitializeComponent();
            m_mindate = DateTime.Today.AddDays(1);
            expirationdate_datepicker.DisplayDate = m_mindate;
            expirationdate_datepicker.DisplayDateStart = m_mindate;
            expirationdate_datepicker.SelectedDate = m_mindate;
            fullname_textbox.Focus();

            DataContext = this;
        }

        public Name Fullname
        {
            get { return new Name(fullname_textbox.Text.Trim()); }
        }

        public Email Email
        {
            get { return new Email(email_textbox.Text.Trim()); }
        }

        public String Comment
        {
            get { return comment_textbox.Text.Trim(); }
        }

        public KeyAlgorithm KeyAlgorithm
        {
            get
            {
                switch (algorithm_combobox.SelectedIndex)
                {
                    case 0: return KeyAlgorithm.DsaELGamal;
                    case 1: return KeyAlgorithm.RsaRsa;
                    default: return KeyAlgorithm.None;
                }
            }
        }

        public UInt32 KeySize
        {
            get { return UInt32.Parse(keysize_combobox.Text); }
        }

        public DateTime ExpirationDate
        {
            get { return expiration_checkbox.IsChecked == true ? DateTime.Today : expirationdate_datepicker.SelectedDate ?? DateTime.Today; }
        }

        private void Generate(Object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void TextChanged(Object sender, TextChangedEventArgs e)
        {
            String fullname = fullname_textbox.Text.Trim();
            String email = email_textbox.Text.Trim();
            generate_button.IsEnabled = GpgApi.Name.IsValid(fullname) && Email.IsValid(email);
        }

        private void DateLostFocus(Object sender, RoutedEventArgs e)
        {
            if (expirationdate_datepicker.SelectedDate == null)
                expirationdate_datepicker.SelectedDate = expirationdate_datepicker.DisplayDateStart;
            else
            {
                DateTime dt = (DateTime)expirationdate_datepicker.SelectedDate;
                if (dt < m_mindate)
                    expirationdate_datepicker.SelectedDate = m_mindate;
            }
        }

        private void SelectionChanged(Object sender, SelectionChangedEventArgs e)
        {
            if (algorithm_combobox != null && keysize_combobox != null)
                if (algorithm_combobox.SelectedIndex == 0 && keysize_combobox.SelectedIndex == 3)
                    keysize_combobox.SelectedIndex = 2;
        }
    }
}
